package com.hzhk.happyrent.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.App;
import com.hzhk.happyrent.Constants;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.bill.ProductBillBean;
import com.hzhk.happyrent.bean.product.OrderListModel.DataBean.OrdersBean;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.OrderCancelDialog;
import com.hzhk.happyrent.ui.activity.order.EditOrderNoActivity;
import com.hzhk.happyrent.ui.activity.order.OrderBuyPayActivity;
import com.hzhk.happyrent.ui.activity.order.OrderReletActivity;
import com.hzhk.happyrent.ui.activity.order.PreOrderActivity;
import com.hzhk.happyrent.ui.activity.PayPeriodsActivity;
import com.hzhk.happyrent.ui.view.PredicateLayout;
import com.hzhk.happyrent.utils.MyPerference;
import com.hzhk.happyrent.utils.TimeUtil;
import com.hzhk.happyrent.utils.glide.ShowImageUtils;
import com.tamic.novate.Throwable;

import java.time.format.TextStyle;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ss on 2017/12/2.
 */

public class ProductOrderAdapter extends BaseAdapter<OrdersBean> {

    private OrderCancelDialog orderCancelDialog;


    public ProductOrderAdapter(Context context) {
        super(context);
    }

    public OrderCancelDialog getOrderCancelDialog() {
        return orderCancelDialog;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_product_orders,
                    null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.setData(getData().get(i));
        return convertView;
    }


    class ViewHolder {
        @BindView(R.id.order_state)
        TextView orderState;
        @BindView(R.id.order_image)
        ImageView orderImage;
        @BindView(R.id.order_title)
        TextView orderTitle;
        @BindView(R.id.product_type)
        PredicateLayout productType;
        @BindView(R.id.order_num)
        TextView orderNum;
        @BindView(R.id.product_order_time)
        TextView productOrderTime;
        @BindView(R.id.product_money)
        TextView productMoney;
        @BindView(R.id.bt_one)
        Button btOne;
        @BindView(R.id.bt_two)
        Button btTwo;
        @BindView(R.id.bt_three)
        Button btThree;
        private String orderStates;
        private String orderCode;
        private String shippingState;
        private String pay_status;
        private String returnState;
        private String money;
        private String orderId;
        private OrdersBean ordersBean;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        public void setData(OrdersBean ordersBean) {
            this.ordersBean = ordersBean;
            ShowImageUtils.showImageViewToCircleNoPlaceHolder(App.getContext(), ordersBean.getGoods_image(), orderImage, 0);
            orderTitle.setText(ordersBean.getGoods_name());
            orderNum.setText(ordersBean.getOrder_code());
            orderStates = ordersBean.getOrder_status();
            orderCode = ordersBean.getId();
            shippingState = ordersBean.getShipping_status();
            pay_status = ordersBean.getPay_status();
            returnState = ordersBean.getReturn_status();
            switch (ordersBean.getOrder_status()) {
                case Constants.ORDER_STATUS_DELETE:
                    orderState.setText("删除");
                    btOne.setVisibility(View.GONE);
                    btTwo.setVisibility(View.GONE);
                    btThree.setVisibility(View.GONE);
                    break;
                case Constants.ORDER_STATUS_BE_CONFIRMED:
                    orderState.setText("待确认");
                    btThree.setText("取消订单");
                    btOne.setVisibility(View.GONE);
                    btTwo.setVisibility(View.GONE);
                    btThree.setVisibility(View.VISIBLE);
                    break;
                case Constants.ORDER_STATUS_AFFIRM:
                    if ("0".equals(ordersBean.getShipping_status())) {
                        if ("0".equals(ordersBean.getPay_status())) {
                            orderState.setText("待支付");
                            btOne.setText("支付");
                            btThree.setText("取消订单");
                            money = ordersBean.getWx_pay_price();
                            orderId = ordersBean.getId();
                            btOne.setVisibility(View.VISIBLE);
                            btTwo.setVisibility(View.GONE);
                            btThree.setVisibility(View.VISIBLE);
                        } else {
                            orderState.setText("待发货");
                            btOne.setVisibility(View.GONE);
                            btTwo.setVisibility(View.GONE);
                            btThree.setVisibility(View.GONE);
                        }

                    } else if ("1".equals(ordersBean.getShipping_status())) {
                        orderState.setText("发货中");
                        btOne.setText("查看物流");
                        btOne.setVisibility(View.GONE);
                        btTwo.setVisibility(View.GONE);
                        btThree.setVisibility(View.GONE);
                    } else {
                        orderState.setText("已确认");
                        btThree.setText("取消订单");
                        btOne.setVisibility(View.GONE);
                        btTwo.setVisibility(View.GONE);
                        btThree.setVisibility(View.VISIBLE);
                    }


                    break;
                case Constants.ORDER_STATUS_CANCEL:
                    orderState.setText("已取消");
                    btOne.setVisibility(View.GONE);
                    btTwo.setVisibility(View.GONE);
                    btThree.setVisibility(View.GONE);
                    break;
                case Constants.ORDER_STATUS_NBRACE:
                    orderState.setText("无效");
                    btOne.setVisibility(View.GONE);
                    btTwo.setVisibility(View.GONE);
                    btThree.setVisibility(View.GONE);
                    break;
                case Constants.ORDER_STATUS_REFUND:
                    orderState.setText("退款中");
                    btOne.setVisibility(View.GONE);
                    btTwo.setVisibility(View.GONE);
                    btThree.setVisibility(View.GONE);
                    break;
                case Constants.ORDER_STATUS_COMPLETE:
                    orderState.setText("已完成");
                    btOne.setVisibility(View.GONE);
                    btTwo.setVisibility(View.GONE);
                    btThree.setVisibility(View.GONE);
                    break;
                case Constants.ORDER_STATUS_INUSE:
                    if ("2".equals(returnState)) {
                        orderState.setText("归还中");
                        btThree.setText("填写邮寄单号");
                        btOne.setVisibility(View.GONE);
                        btTwo.setVisibility(View.GONE);
                        btThree.setVisibility(View.VISIBLE);
                    } else if ("3".equals(returnState)) {
                        orderState.setText("寄回中");
                        btOne.setVisibility(View.GONE);
                        btTwo.setVisibility(View.GONE);
                        btThree.setVisibility(View.GONE);
                    } else {
                        orderState.setText("使用中");
                        btOne.setText("购买");
                        btTwo.setText("续租");
                        btThree.setText("预约还机");
                        btOne.setVisibility(View.VISIBLE);
                        btTwo.setVisibility(View.VISIBLE);
                        btThree.setVisibility(View.VISIBLE);
                    }
                    break;

            }
            ordersBean.setOrder_state_current(orderState.getText().toString());
            if (!TextUtils.isEmpty(ordersBean.getGoods_sku_spec())) {
                productType.removeAllViews();
                String description = ordersBean.getGoods_sku_spec().substring(1, ordersBean.getGoods_sku_spec().length() - 1);
                if (description.contains("][")) {
                    String[] tag = description.split("]\\u005B");
                    for (String s : tag) {
                        TextView textView = new TextView(mContext);
                        textView.setText(s);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                        textView.setPadding(5, 0, 5, 0);
                        textView.setTextColor(mContext.getResources().getColor(R.color.tab_red_80));
                        //textView.setBackgroundResource(R.drawable.bg_red_hollow);
                        textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_red_hollow));
                        productType.addView(textView);
                    }
                } else {
                    TextView textView = new TextView(mContext);
                    textView.setText(description);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                    textView.setPadding(5, 0, 5, 0);
                    textView.setTextColor(mContext.getResources().getColor(R.color.tab_red_80));
                    //textView.setBackgroundResource(R.drawable.bg_red_hollow);
                    textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_red_hollow));
                    productType.addView(textView);
                }
            }
            productMoney.setText(App.getContext().getString(R.string.price_lg) + ordersBean.getPrice());
            productOrderTime.setText(TimeUtil.getStrTime(ordersBean.getCreate_date()));
        }
        private void initIntent(Intent intent) {
            intent.putExtra(Constants.ORDER_ID,ordersBean.getId());
            //intent.putExtra(Constants.ORDER_ADDRESS,orderDetailModel.getData().getAddress());
            intent.putExtra(Constants.TOTAL_PRICE,ordersBean.getTotal_price());
            intent.putExtra(Constants.ATTR_KEY_PATH,ordersBean.getGoods_sku_spec());
            intent.putExtra(Constants.GOODS_NAME,ordersBean.getGoods_name());
            intent.putExtra(Constants.GOODS_IMAGE,ordersBean.getGoods_image());
            //intent.putExtra(Constants.CONSIGNEE,orderDetailModel.getData().getConsignee());
            //intent.putExtra(Constants.MOBILE,orderDetailModel.getData().getMobile());
        }

        @OnClick({R.id.bt_one, R.id.bt_two, R.id.bt_three})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.bt_one:
                    switch (orderStates){
                        case Constants.ORDER_STATUS_AFFIRM:
                            if ("0".equals(pay_status)) {
                                //已确认-待支付
                                Intent intent = new Intent(mContext, PayPeriodsActivity.class);
                                intent.putExtra("pay_total", money);
                                intent.putExtra("order_id", orderId);
                                intent.putExtra("come_from", "order_list");
                                mContext.startActivity(intent);
                            } else if ("1".equals(shippingState)) {
                                //查看物流

                            }
                            break;
                        //使用中-购买
                        case Constants.ORDER_STATUS_INUSE:
                            //buyOut();
                            Intent intent = new Intent(mContext, OrderBuyPayActivity.class);
                            initIntent(intent);
                            mContext.startActivity(intent);
                            break;
                    }
                    break;
                case R.id.bt_two:
                    switch (orderStates) {
                        //使用中-续租
                        case Constants.ORDER_STATUS_INUSE:
                            Intent intent=new Intent(mContext, OrderReletActivity.class);
                            intent.putExtra("order_id",orderCode);
                            intent.putExtra("img_product",ordersBean.getGoods_image());
                            intent.putExtra("product_title",ordersBean.getGoods_name());
                            intent.putExtra("product_description",ordersBean.getGoods_sku_spec());
                            intent.putExtra("product_value",ordersBean.getTotal_price());
                            mContext.startActivity(intent);
                            break;
                    }
                    break;
                case R.id.bt_three:
                    switch (orderStates) {
                        //未确认-取消订单
                        case Constants.ORDER_STATUS_BE_CONFIRMED:
                            orderCancelDialog = new OrderCancelDialog(mContext, orderCode);
                            break;
                        //已确认-取消订单
                        case Constants.ORDER_STATUS_AFFIRM:
                            orderCancelDialog = new OrderCancelDialog(mContext, orderCode);
                            break;
                        case Constants.ORDER_STATUS_INUSE:
                            if ("2".equals(returnState)) {
                                //归还中-填写邮寄单号
                                Intent intent = new Intent(mContext, EditOrderNoActivity.class);
                                initIntent(intent);
                                mContext.startActivity(intent);
                            } else {
                                //使用中-预约还机
                                Intent intent = new Intent(mContext, PreOrderActivity.class);
                                initIntent(intent);
                                mContext.startActivity(intent);
                            }
                            break;
                    }
                    break;
            }
        }

    }

}
